const puppeteer = require('puppeteer-core');
const path = require('path');
const {
    chromePath,
} = require('./ti_config');

(async () => {
    browser = await puppeteer.launch({
        headless: false,
        executablePath: path.resolve(chromePath),
        ignoreHTTPSErrors : true,   // 忽略证书错误
        ignoreDefaultArgs:['--enable-automation'],  // 去掉左上角 Chrome 正受自动软件控制
        //devtools: true,//打开控制台
        defaultViewport: null,//自动适应大小
        args:[
            '--window-size=1720,980',
        ],
    });

    page = await browser.newPage();

    //伪装，防止被判断是爬虫
    //await page.setUserAgent(userAgent);
    await page.evaluateOnNewDocument(() => {
        Object.defineProperty(navigator, 'webdriver', {
            get: () => undefined,
        });
        // Object.defineProperty(navigator, 'languages', {
        //     get: () => ['zh-CN', 'zh', 'en'],
        // });
    });
    await page.evaluateOnNewDocument(() => {
        Object.defineProperty(navigator, 'plugins', {
            get: () => [
                {
                    0: {type: "application/x-google-chrome-pdf", suffixes: "pdf", description: "Portable Document Format", enabledPlugin: Plugin},
                    description: "Portable Document Format",
                    filename: "internal-pdf-viewer",
                    length: 1,
                    name: "Chrome PDF Plugin"
                },
                {
                    0: {type: "application/pdf", suffixes: "pdf", description: "", enabledPlugin: Plugin},
                    description: "",
                    filename: "mhjfbmdgcfjbbpaeojofohoefgiehjai",
                    length: 1,
                    name: "Chrome PDF Viewer"
                },
                {
                    0: {type: "application/x-nacl", suffixes: "", description: "Native Client Executable", enabledPlugin: Plugin},
                    1: {type: "application/x-pnacl", suffixes: "", description: "Portable Native Client Executable", enabledPlugin: Plugin},
                    description: "",
                    filename: "internal-nacl-plugin",
                    length: 2,
                    name: "Native Client"
                }
            ],
        });
    });
    await page.evaluateOnNewDocument(() => {
        window.chrome.runtime = {
            "OnInstalledReason":{"CHROME_UPDATE":"chrome_update","INSTALL":"install","SHARED_MODULE_UPDATE":"shared_module_update","UPDATE":"update"},
            "OnRestartRequiredReason":{"APP_UPDATE":"app_update","OS_UPDATE":"os_update","PERIODIC":"periodic"},
            "PlatformArch":{"ARM":"arm","ARM64":"arm64","MIPS":"mips","MIPS64":"mips64","X86_32":"x86-32","X86_64":"x86-64"},
            "PlatformNaclArch":{"ARM":"arm","MIPS":"mips","MIPS64":"mips64","X86_32":"x86-32","X86_64":"x86-64"},
            "PlatformOs":{"ANDROID":"android","CROS":"cros","LINUX":"linux","MAC":"mac","OPENBSD":"openbsd","WIN":"win"},
            "RequestUpdateCheckStatus":{"NO_UPDATE":"no_update","THROTTLED":"throttled","UPDATE_AVAILABLE":"update_available"},
            "id":function(){},
            "sendMessage":function(){},
            "connect":function(){},
        };
    });
})();

